package com.zzh.holland;

import com.zzh.mymath.MyMath;

import java.util.Arrays;

/*荷兰国旗问题*/
public class Holland {
    public static void main(String[] args) {
        int[] a = new int[]{1, 0, 2, 0, 1, 2, 1, 0, 1, 2, 0, 1, 0};
        int begin = 0;
        int current = 0;
        int end = a.length - 1;
        while (current <= end) {
            if (a[current] == 2) {//=2
                MyMath.swap(a, current, end--);
            } else if (a[current] == 1) {//=1
                current++;
            } else {//=0
                if (begin == current) {
                    begin++;
                    current++;
                } else {
                    MyMath.swap(a, begin++, current++);
                }
            }
        }
        System.out.println(Arrays.toString(a));
    }


}
